﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IceDog.EFCore.Utils;
using IceDog.EFCore.WebApiServer.Context;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;

namespace IceDog.EFCore.WebApiServer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                //HH:mm:ss 按照24小时制的格式进行字符串格式化
                //hh:mm:ss  按照12小时制的格式进行字符串格式化
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            });
            //配置mysql连接字符串
            services.AddDbContextPool<DioBlogContext>(options => options.UseMySql(ConnectionStringHelper.MySql));
            services.AddSwaggerGen(options =>
            {
                var title = "Blog Web Api";
                var license = new OpenApiLicense()
                {
                    Name = "GPL V3",
                    Url = new Uri("https://www.example.com/gpl_v3.html")
                };
                var contact = new OpenApiContact()
                {
                    Name = "DHclly",
                    Email = "335121817@qq.com",
                    Url = new Uri("https://www.example.com/contact_me.html")
                };
                var description = "this project use for test how to use swashbuckle";
                options.SwaggerDoc("v1", new OpenApiInfo
                {

                    Title = title,
                    Version = "v1.0",
                    License = license,
                    Contact = contact,
                    Description = description
                });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
            app.UseSwagger(options =>
            {

            });
            app.UseSwaggerUI(options =>
            {
                //把swagger UI上选择的版本和json文档关联起来
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "version 1.0");
            });
        }
    }
}
